<template>
  <div class="department-container">
    <div class="app-container">
      <PageTools>
        <template #right>
          <el-button type="primary" @click="addHandler('',1)">添加权限</el-button>
        </template>
      </PageTools>
      <el-card style="margin-top: 15px">
        <el-table
          :data="tableData"
          style="width: 100%; margin-bottom: 20px"
          row-key="id"
          border
          default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <div style="text-align:center">
                <el-button v-if="(+scope.row.pid)===0" size="mini" type="primary" @click="addHandler(scope.row.id,scope.row.type+1)">新增</el-button>
                <el-button type="success" size="mini" @click="editHandler(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" @click="delHandler(scope.row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible.sync="showDialog" :title="isEdit?'编辑':'新增'">
      <perDia v-if="showDialog" ref="perDia" :is-edit="isEdit" :row="row" :show-dialog.sync="showDialog" @success="success" />
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, delPermission } from '@/api/permisson'
import { transArray } from '@/utils'
import perDia from './perDia.vue'

export default {
  components: {
    perDia
  },
  data() {
    return {
      showDialog: false,
      row: {},
      isEdit: false,
      tableData: []
    }
  },
  created() {
    this.uploadPermissionList()
  },
  methods: {
    async uploadPermissionList() {
      const res = await getPermissionList()
      // console.log(res)
      if (res.code !== 10000) return
      this.tableData = transArray(res.data, '')
      // console.log(this.tableData)
    },
    // 添加
    addHandler(pid, type) {
      this.isEdit = false
      // console.log(pid)
      this.showDialog = true
      // 结构没有加载完拿不到组件
      // $nextTick
      // 定时器
      this.$nextTick(() => {
        this.$refs.perDia.formData.pid = pid
        this.$refs.perDia.formData.type = type
      })
    },
    // 成功回调
    success() {
      this.uploadPermissionList()
    },
    // 删除
    delHandler(id) {
      this.$confirm('删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delPermission(id)
        // console.log(res)
        if (res.code !== 10000) return
        this.uploadPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editHandler(row) {
      this.isEdit = true
      this.showDialog = true
      this.row = row
    }
  }
}
</script>
